package com.lnsystem.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.net.ftp.FTPClient; 
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.lnsystem.service.SymbolsService;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.IOException; 
import java.io.FileOutputStream; 
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
@Component 
public class SymbolsServiceFTPImpl implements SymbolsService{
	
	private static final int CHUNK_SIZE = 1000; //this is the number of quotes downloaded in a single TK API call.
  
	private static Logger log = Logger.getLogger(SymbolsServiceFTPImpl.class);
	Properties prop;
	
	public void loadProperties() throws IOException{
		prop = new Properties();
		ClassLoader loader = Thread.currentThread().getContextClassLoader();           
		InputStream stream = loader.getResourceAsStream("ftpservice.properties");
		prop.load(stream);
	}
	
  public static void main(String[] args) { 	
	  SymbolsServiceFTPImpl s = new SymbolsServiceFTPImpl();
	  List<String> allSymbols = s.getAllSymbols(false);
	  log.info(allSymbols.size());
  }

  public List<String> getSymbolsFile(String dataFileName){
    FTPClient ftp = new FTPClient(); 
    PrintWriter fos = null; 
	List<String> symbols = new ArrayList<String>();
	String delims = "[|]+";

    try {
    	
    	fos = new PrintWriter(new FileWriter("/tmp/lnsystem-symbols-file.txt"));
    	
    	if(prop == null){
    		loadProperties();
    	}
 
    	ftp.connect(prop.getProperty("server"));
    	log.info(ftp.getReplyCode()+"-"+ftp.getReplyString());

    	// Pass username and password 
    	ftp.login(prop.getProperty("username"), prop.getProperty("password")); 
    	log.info(ftp.getReplyCode()+"-"+ftp.getReplyString());
    	
    	InputStream is = (InputStream) ftp.retrieveFileStream(dataFileName);
    	log.info(ftp.getReplyCode()+"-"+ftp.getReplyString());
      
		Scanner sc = new Scanner( is );
		
		//read first line and discard as it is header line..
		sc.nextLine();
	      
	      while(sc.hasNextLine()){
		      String input = sc.nextLine(); 
			    String[] dataElements = input.split(delims);
			    
			    //ignore any symbol that has a "$" in the symbol.
			    // also ignore the last line in the symbols file.
			    if(dataElements[0]!=null && (!dataElements[0].contains("$")) && (!dataElements[0].contains("File Creation"))) {
				    symbols.add(dataElements[0]);
			    }
			    
			    //fos.println(input);
			    fos.println(dataElements[0]);
			    
		      //log.info(input);SymbolsService
	      }

	      sc.close();
	      is.close();

    } catch (IOException e) { 
      e.printStackTrace(); 
      log.fatal("Exception occured: "+ExceptionUtils.getStackTrace(e));
    } finally { 
      try { 
        if (fos != null) { 
          fos.close(); 
        } 
        ftp.disconnect(); 
        //log.info(ftp.getReplyCode()+"-"+ftp.getReplyString());
      } catch (IOException e) {
        e.printStackTrace(); 
        log.fatal("Exception occured: "+ExceptionUtils.getStackTrace(e));
      } 
    }
    return symbols;
  }
  
  
  /**
   * {@inheritDoc} 
   */
	public List<String> getAllSymbols(boolean isForMultiQuotesURL) {
		
		List<String> allSymbols = new ArrayList<String>();
		try{
	    	if(prop == null){
	    		loadProperties();
	    	}
	  	      List<String> symbols;
	  	      String[] files = prop.getProperty("dataFile").split(",");
	  	      for(String dataFileName : files){
	  	    	  symbols = getSymbolsFile(dataFileName);
	  	    	  //log.info(symbols);
	  	    	  //log.info(s+" has "+symbols.size()+" symbols.");
	  	    	  allSymbols.addAll(symbols);
	  	      }
	  	      
	  	      Collections.sort(allSymbols);
	  	      
	  	      if(isForMultiQuotesURL) {
	  	    	  
	  	    	  List<String> symbolsChunk = new ArrayList<String>();
	  	    	  List<String> tempSymbolsList = new ArrayList<String>();
	  	    	  int i = 1;
	  	    	  for(String s : allSymbols) {
	  	    		  if(i%CHUNK_SIZE == 0) {
	  	    			String commaSeperatedList = StringUtils.join(tempSymbolsList,",");
	  	    			symbolsChunk.add(commaSeperatedList);
	  	    			i=1;
	  	    			tempSymbolsList = new ArrayList<String>();
	  	    		  } else {
	  	    			tempSymbolsList.add(s);
	  	    			i++;
	  	    		  }
	  	    	  }
	  	    	  if(tempSymbolsList.size()>0) {
		    			String commaSeperatedList = StringUtils.join(tempSymbolsList,",");
		    			symbolsChunk.add(commaSeperatedList);
	  	    	  }
	  	    	  return symbolsChunk;
	  	    	  
	  	      } else {
	  	    	return allSymbols;
	  	      }
	  	      
			
		} catch(Exception e){
			e.printStackTrace();
			log.fatal("Exception occured: "+ExceptionUtils.getStackTrace(e));
		}
		Collections.sort(allSymbols);
		return allSymbols;
	} 

} 
